package com.yyk.yykywd

import android.content.Context
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.yyk.yykywd.listenter.DialogSureCancelListenter
import com.yyk.yykywd.listenter.TakePhotoListenter
import com.yyk.yykywd.mvvm.adapter.AddMaterialAdapter
import com.yyk.yykywd.util.DialogUtil
import com.yyk.yykywd.util.TakePhotoHelper
import com.yyk.yykywd.util.ToastUtil
import com.yyk.yykywd.widght.LoadingLayout
import org.devio.takephoto.app.TakePhoto
import java.text.SimpleDateFormat
import java.util.*

/**
 * 文件描述：
 * Created by guo on 2019-11-19.
 */

//获取控件上显示的文字
fun getTextString(view: TextView): String {
    return view.text.trim().toString()
}

fun listToString(mutableList: MutableList<String>): String {
    val joinTo = mutableList.joinTo(StringBuilder(), ",")
    return joinTo.toString()
}

fun stringToList(s: String): List<String> {
    return s.split(",")
}

fun setLoadingLayoutError(loadingLayout: LoadingLayout, context: Context) {
    loadingLayout.showState(context.getString(R.string.error_text))
}

fun getTime(date: Date, pattern: String): String { //可根据需要自行截取数据显示
    val format = SimpleDateFormat(pattern)
    return format.format(date)
}

fun show(context: Context, title: String, message: String, sureTitle: String, cancelTitle: String, mListener: (() -> Unit)) {
    DialogUtil.showReturnTips(context, title, message, sureTitle, cancelTitle, object : DialogSureCancelListenter {
        override fun onCancelListenter() {
        }

        override fun onSureListenter() {
            mListener.invoke()
        }

    }, false)
}

fun newCustomer(context: Context, mListener: () -> Unit) {
    show(context, "是否确认新增客户信息", "保存后，可在\"工作-意向客户\"中跟进此客户", "确认", "取消") {
        mListener()
    }
}

fun addSeas(context: Context, mListener: () -> Unit) {
    show(context, "是否确认将客户添加至公海", "添加至公海后，则失去该客户的归属权，其他人员均可认领", "确认", "取消") {
        mListener()
    }
}

fun submitRefund(context: Context,message: String, mListener: () -> Unit) {
    show(context, "是否确认提交退款？",
            message,
            "确认",
            "取消") {
        mListener()
    }
}

fun showAlreadyAdd(context: Context, type: String) {
    val title: String
    if (type == HOSPITAL) {
        title = "此医院已被添加"
    } else {
        title = "此代理商已被添加"
    }
    show(context, title, "可在\"客户\"中查询业务归属人及相关信息", "", "好的") {

    }
}

fun backLogic(context: Context, mListener: () -> Unit) {
    show(context, "", "填写的信息尚未保存，是否确认返回", "确认返回", "取消") {
        mListener.invoke()
    }
}

fun <T : ViewModel> getViewModel(activity: FragmentActivity, modelClass: Class<T>): T {
    return ViewModelProviders.of(activity).get(modelClass)
}

fun <T : ViewModel> getFragmentViewModel(fragment: Fragment, modelClass: Class<T>): T {
    return ViewModelProviders.of(fragment).get(modelClass)
}

fun setAdapterChildClickListener(context: Context,
                                 adapter: AddMaterialAdapter,
                                 takePhoto: TakePhoto?,
                                 limit: Int,
                                 uploadClickListener: UploadClickListener) {
    adapter.setOnItemChildClickListener { position, view ->
        when (view.id) {
            R.id.iv -> {
                uploadClickListener.clickIv()
                showTakePhotoWithLimit(context, takePhoto, limit)
            }
            R.id.iv_remove -> {
                uploadClickListener.clickRemove(position)
            }
        }
    }
}

interface UploadClickListener {
    fun clickIv()
    fun clickRemove(position: Int)
}

fun showTakePhotoWithLimit(context: Context, takePhoto: TakePhoto?, limit: Int) {
    DialogUtil.showTakePhoto(context, object : TakePhotoListenter {
        override fun onCamera() {
            TakePhotoHelper.takePhotoOperation(takePhoto, CAMERA, limit)
        }

        override fun onPic() {
            TakePhotoHelper.takePhotoOperation(takePhoto, GALLERY, limit)
        }
    })
}

fun getSiteType(status: Int): String {
    var siteType = ""
    when (status) {
        1 -> {
            siteType = "大型ID站点"
        }
        2 -> {
            siteType = "中型ID站点"
        }
        3 -> {
            siteType = "小型ID站点"
        }
    }
    return siteType
}

fun getContractType(flag: Int): String {
    var contractType = ""
    when (flag) {
        1 -> {
            contractType = "全款合同"
        }
        2 -> {
            contractType = "定金合同"
        }
    }
    return contractType
}

fun getPersonName(name: String?, getName: String?): String {
    if (name.isNullOrEmpty()) {
        return "认领人：${getName}"
    } else {
        return "创建人：${name}"
    }
}

fun getTime(name: String?, createTime: Int, claimTime: Int): String {
    if (name.isNullOrEmpty()) {
        return "认领时间：${getCommonTime(claimTime)}"
    } else {
        return "创建时间：${getCommonTime(createTime)}"
    }
}

fun hideRefreshAndLoadMore(srl: SmartRefreshLayout) {
    srl.finishRefresh()
    srl.finishLoadMore()
}

fun <T> showStateLayout(adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>, code: Int, list: MutableList<T>, msg: String, loadingLayout: LoadingLayout) {
    if (code == 200) {
        adapter.notifyDataSetChanged()
        loadingLayout.showContent()
        return
    }
    if (list.size == 0) {
        loadingLayout.showEmpty(R.mipmap.img_zwxgsj, "暂无数据")
        adapter.notifyDataSetChanged()
    } else {
        loadingLayout.showContent()
        ToastUtil.showShort(msg)
    }
}

fun main() {
    val list = ""
    println(stringToList(list))
}